<?php

/**
 * Description of ArctypeController
 *
 * @author zw
 * time: 2014-8-15
 */

namespace Admin\Controller;

use Think\Controller;

Class ArctypeController extends Controller {

    public function index() {
        //获取所有栏目树状列表
        $arctype = D('Arctype');
        $rsArr = M('arctype')->getField('id,pid,title');
        $categroy = $arctype->getMenuTree($rsArr, 0, 0);

        $this->assign('categroy', $categroy);
        $this->display();
    }

    //添加栏目
    public function add() {
        $m = M('arctype');
        if (IS_POST) {
            $data = $_POST;
            if ($m->add($data) > 0) {
                $this->success('添加栏目成功');
            } else {
                $this->error('添加栏目失败');
            }
        } else {
            //获取当前栏目的上级栏目id
            $pid = isset($_GET['pid']) ? $_GET['pid'] : 0;
            $parentid = $m->where("id=$pid")->getField('id');

            //获取所有栏目树状列表
            $arctype = D('Arctype');
            $rsArr = $m->getField('id,pid,title');
            $categroy = $arctype->getMenuTree($rsArr, 0, 0);

            //获取模型列表
            $m2 = M('model');
            $mlist = $m2->where("isshow=1")->field('id,name')->select();



            $this->assign('mlist', $mlist); //模型列表
            $this->assign('tmplist', getTpl('list')); //列表模板
            $this->assign('cntlist', getTpl('cont')); //内容模板
            $this->assign('parentid', $parentid); //当前栏目上级栏目
            $this->assign('categroy', $categroy); //栏目树状图
            $this->display();
        }
    }

    //编辑栏目
    public function edit() {
        $m = M('arctype');

        if (IS_POST) {
            $data = $_POST;
            $id = $_POST['id'];
            if (isset($id)) {
                if ($m->where("id=$id")->save($data) > 0) {
                    $this->success('栏目编辑成功');
                } else {
                    $this->error('栏目编辑失败');
                }
            }
        } else {
            //获取所有栏目树状列表
            $arctype = D('Arctype');
            $rsArr = M('arctype')->getField('id,pid,title');
            $categroy = $arctype->getMenuTree($rsArr, 0, 0);

            $id = isset($_GET['id']) ? $_GET['id'] : 0;
            if ($id != 0) {
                $rsArr = $m->where("id=$id")->find();
            }

            //获取模型列表
            $m2 = M('model');
            $mlist = $m2->where("isshow=1")->field('id,name')->select();


            $this->assign('mlist', $mlist); //模型列表
            $this->assign('tmplist', getTpl('list')); //列表模板
            $this->assign('cntlist', getTpl('cont')); //内容模板
            $this->assign('id', $id);
            $this->assign('categroy', $categroy);
            $this->assign('rsArr', $rsArr);
            $this->display();
        }
    }

    //删除栏目
    public function del($id) {
        $m = M('arctype');
        $count = $m->where("pid=$id")->count();
        if ($count > 0) {
            $this->error('请先删除该栏目的子栏目');
        } else {
            if ($m->where("id=$id")->delete()) {
                $this->success('删除栏目成功');
            } else {
                $this->error('删除栏目失败');
            }
        }
    }

}

?>
